<template>
	<view class="card-list deepcover">
		<u-popup class="test" v-model="show" mode="bottom" border-radius="32" :mask="false" height="900" @open="onOpen"
		 @close="onClose">
			<view class="container flex-col justify-between align-center">
				<scroll-view scroll-y="true" style="height: 700rpx;">
					<view class="title">卡片版式</view>
					<view class="list">
						<view class="item" :class="{ on: onformat == i.id }" :style="{ backgroundImage: 'url(' +  i.thumbnail + ')', backgroundColor: i.bg_color }"
						 v-for="i in format" :key="i.id" @click="setStyle('format', i.id)">
							<image src="../../../static/images/choice.png" class="choice" mode=""></image>
						</view>
					</view>

					<view class="title">卡片背景</view>
					<view class="list">
						<view class="item" :class="{ on: onhead == i.id }" :style="{ backgroundImage: 'url(' +  i.thumbnail + ')', backgroundColor: i.bg_color }"
						 v-for="i in head" :key="i.id" @click="setStyle('head', i.id)">
							<image src="../../../static/images/choice.png" class="choice" mode=""></image>
						</view>
					</view>

					<view class="title">信息背景</view>
					<view class="list">
						<view class="item" :class="{ on: oninfo == i.id }" :style="{ backgroundImage: 'url(' +  i.thumbnail + ')', backgroundColor: i.bg_color }"
						 v-for="i in info" :key="i.id" @click="setStyle('info', i.id)">
							<image src="../../../static/images/choice.png" class="choice" mode=""></image>
						</view>
					</view>
				</scroll-view>

				<my-btn radius @click="postStyle">确定</my-btn>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		props: {
			open: {
				type: Boolean,
				default: false
			},
			cardId: {
				type: String,
				default: ''
			},
			list: {
				type: Array,
				default: []
			}
		},
		data() {
			return {
				show: false,
				format: [],
				head: [],
				info: [],
				style: [{
						id: '',
						thumbnail: '',
						type: 'format'
					},
					{
						id: '',
						thumbnail: '',
						type: 'head'
					},
					{
						id: '',
						thumbnail: '',
						type: 'info'
					}
				]
			};
		},
		watch: {
			list(val) {
				this.initStyle()
			}
		},
		computed: {
			onformat() {
				return this.style.find(item => item.type == 'format').id
			},
			onhead() {
				return this.style.find(item => item.type == 'head').id
			},
			oninfo() {
				return this.style.find(item => item.type == 'info').id
			},
		},
		watch: {
			open(val) {
				this.show = val
			}
		},
		mounted() {
			this.getStyle()
			this.initStyle()
		},
		methods: {
			getStyle() {
				this.$u.get('/myvcard/style').then(res => {
					let {
						format,
						head,
						info
					} = res.data.result.style
					this.format = format
					this.head = head
					this.info = info
				})
			},
			initStyle() {
				for (let i = 0; i < this.list.length; i++) {
					let target = this.list[i]
					this.setStyle(target.style_type, target.style_id)
				}
			},
			setStyle(type, id) {
				this.style.find(item => item.type == type).id = id
			},
			postStyle() {
				this.$u.post('/myvcard/style/set', {
					id: this.cardId,
					style: this.style
				}).then(res => {
					this.show = false
					this.$msg(res.data.msg)
					this.$emit('setStyle')
				})
			},
			onOpen() {
				this.$emit('toggle', this.show)
			},
			onClose() {
				this.$emit('toggle', this.show)
			}
		}
	}
</script>

<style lang="scss">
	.container {
		padding: 40rpx 0;
		height: 100%;
		box-sizing: border-box;
		position: relative;

		.title {
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #333333;
			padding: 0 40rpx;
		}

		.list {
			display: flex;
			flex-flow: row wrap;
			justify-content: flex-start;
			align-items: center;
			padding: 24rpx 38rpx;
		}

		.item {
			width: 148rpx;
			height: 88rpx;
			box-sizing: border-box;
			background-position: center;
			background-size: cover;
			margin: 10rpx;
			border-radius: 4rpx;
			overflow: hidden;
			
			.choice {
				width: 100%;
				height: 100%;
				opacity: 0;
				transition: all 0.3s;
			}

			&.on {
				border: 1px solid #0481ff;
				.choice {
					opacity: 1;
				}
			}
		}
	}
</style>
